import 'package:flutter/material.dart';
import 'risk_assessment_result_page.dart';

class RiskAssessmentPage extends StatefulWidget {
  const RiskAssessmentPage({Key? key}) : super(key: key);

  @override
  State<RiskAssessmentPage> createState() => _RiskAssessmentPageState();
}

class _RiskAssessmentPageState extends State<RiskAssessmentPage> {
  String _gender = '女性';
  final TextEditingController _ageController = TextEditingController();
  final TextEditingController _currentHistoryController =
      TextEditingController();
  final TextEditingController _dietController = TextEditingController();
  final TextEditingController _sleepController = TextEditingController();
  final TextEditingController _exerciseController = TextEditingController();

  @override
  void dispose() {
    _ageController.dispose();
    _currentHistoryController.dispose();
    _dietController.dispose();
    _sleepController.dispose();
    _exerciseController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              padding: const EdgeInsets.only(
                  left: 20, right: 20, bottom: 20, top: 10),
              color: const Color(0xFF1DC4A0),
              child: Row(
                children: [
                  IconButton(
                    icon: const Icon(Icons.arrow_back, color: Colors.white),
                    onPressed: () => Navigator.pop(context),
                    padding: EdgeInsets.zero,
                  ),
                  const SizedBox(width: 10),
                  const Text(
                    '风险评估',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.all(20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        '用户日志',
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 20),
                      const Text('年龄',
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold)),
                      const SizedBox(height: 10),
                      TextField(
                        controller: _ageController,
                        keyboardType: TextInputType.number,
                        decoration: _inputDecoration('请输入年龄'),
                      ),
                      const SizedBox(height: 20),
                      const Text('性别',
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold)),
                      const SizedBox(height: 10),
                      _buildDropdownButton(
                        value: _gender,
                        items: const ['女性', '男性'],
                        onChanged: (value) => setState(() => _gender = value!),
                      ),
                      const SizedBox(height: 20),
                      const Text('现病史',
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold)),
                      const SizedBox(height: 10),
                      TextField(
                        controller: _currentHistoryController,
                        maxLines: 3,
                        decoration: _inputDecoration('请描述现病史'),
                      ),
                      const SizedBox(height: 20),
                      const Text('饮食情况',
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold)),
                      const SizedBox(height: 10),
                      TextField(
                        controller: _dietController,
                        maxLines: 3,
                        decoration: _inputDecoration('描述饮食习惯'),
                      ),
                      const SizedBox(height: 20),
                      const Text('睡眠情况',
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold)),
                      const SizedBox(height: 10),
                      TextField(
                        controller: _sleepController,
                        maxLines: 3,
                        decoration: _inputDecoration('描述睡眠情况'),
                      ),
                      const SizedBox(height: 20),
                      const Text('运动情况',
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold)),
                      const SizedBox(height: 10),
                      TextField(
                        controller: _exerciseController,
                        maxLines: 3,
                        decoration: _inputDecoration('描述运动习惯'),
                      ),
                      const SizedBox(height: 30),
                      SizedBox(
                        width: double.infinity,
                        child: ElevatedButton(
                          onPressed: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => RiskAssessmentResultPage(
                                  age: _ageController.text,
                                  gender: _gender,
                                  currentHistory:
                                      _currentHistoryController.text,
                                  diet: _dietController.text,
                                  sleep: _sleepController.text,
                                  exercise: _exerciseController.text,
                                ),
                              ),
                            );
                          },
                          style: ElevatedButton.styleFrom(
                            backgroundColor: const Color(0xFF1DC4A0),
                            padding: const EdgeInsets.symmetric(vertical: 15),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10)),
                          ),
                          child: const Text(
                            '开始评估',
                            style: TextStyle(fontSize: 18, color: Colors.white),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  InputDecoration _inputDecoration(String hint) => InputDecoration(
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide.none,
        ),
        filled: true,
        fillColor: Colors.grey.shade100,
        hintText: hint,
      );

  Widget _buildDropdownButton({
    required String value,
    required List<String> items,
    required ValueChanged<String?> onChanged,
  }) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 15),
      decoration: BoxDecoration(
        color: Colors.grey.shade100,
        borderRadius: BorderRadius.circular(10),
      ),
      child: DropdownButton<String>(
        value: value,
        isExpanded: true,
        underline: const SizedBox(),
        items: items
            .map((item) => DropdownMenuItem(value: item, child: Text(item)))
            .toList(),
        onChanged: onChanged,
      ),
    );
  }
}
